package lc;
import java.util.*;
import org.junit.*;
public class Ex71 {
    class Solution1 {
        public String simplifyPath(String path) {
            Stack<String> stack = new Stack<>();
            char[] cs = path.toCharArray();
            int len = cs.length;
            for (int i = 0; i < len; i++) {
                if (cs[i] == '.') {
                    if (i < len - 1 && cs[i + 1] == '.') {
                        if (i < len - 2 && cs[i + 2] == '.') {
                            int j = i;
                            StringBuilder sb = new StringBuilder();
                            while (j < len && cs[j] == '.') {
                                sb.append(".");
                                j++;
                            }
                            i = j - 1;
                            stack.push(sb.toString());
                            continue;
                        }
                        stack.pop();
                        if (!stack.isEmpty()) {
                            stack.pop();
                        }
                    }
                    i++;
                } else if (cs[i] == '/')  {
                    if (!stack.isEmpty() && "/".equals(stack.peek())) {
                        continue;
                    } else {
                        stack.push("/");
                    }
                } else {
                    StringBuilder sb = new StringBuilder();
                    int j = i;
                    while (j < len && (cs[j] >= 'a' && cs[j] <= 'z' || cs[j] >= '0' && cs[j] <= '9' || cs[j] == '_')) {
                        sb.append(cs[j]);
                        j++;
                    }
                    i = j - 1;
                    stack.push(sb.toString());
                }
            }
            if (!stack.isEmpty() && "/".equals(stack.peek())) {
                stack.pop();
            }
             StringBuilder sb = new StringBuilder();
    
            while (!stack.isEmpty()) {
                sb.insert(0, stack.pop());
            }
            return sb.length() > 0 ? sb.toString() : "/";
        }
    }

    class Solution {
        public String simplifyPath(String path) {
            //不统计/，每次在遇到/统计两个/之间的内容。
            char[] cs = path.toCharArray();
            int len = cs.length;
            Stack<String> stack = new Stack<>();
            StringBuilder sb = null;
            for (int i = 0; i < len; i++) {
                if (cs[i] != '/') {
                    sb = new StringBuilder();
                    int j = i;
                    while (j < len && (j == '.' || isValid(cs[j]))) {
                        sb.append(cs[j]);
                        j++;
                    }
                    i = j - 1;
                    String s = sb.toString();
                    if (".".equals(s)) {
                        continue;
                    } else if ("..".equals(s) && !stack.isEmpty()) {
                        stack.pop();
                    } else {
                        stack.push(s);
                    }
                }
            }
            sb = new StringBuilder();
            while (!stack.isEmpty()) {
                sb.insert(0, stack.pop());
                sb.insert(0, '/');
            }
            return sb.toString();
        }

        public boolean isValid(char c) {
            return c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '_';
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.simplifyPath("/..."));     
        System.out.println(s.simplifyPath("/a/./b//../../c/"));        

        System.out.println(s.simplifyPath("/a/./b//../../c/"));        
        System.out.println(s.simplifyPath("/a/./b//../../c/"));        

    }
}
